import Vue from 'vue'
import base from '@/config/baseUrl';
import vuex from '@/store';
import {
	router as $Router
} from '@/router/index.js'


let BASE_URL = base.baseUrl

export default (params) => {
	
	if(!params.noLoading){
		// 加载中
		uni.showLoading({
		    title: '加载中'
		})
	}
	// BASE_URL = BASE_URL + '/api'
  
	let defaultData ={
		app_type : "app",
		app_type_name : "app",
		token:  uni.getStorageSync('token'),
	}
	
	// // #ifdef H5
	// BASE_URL='/api'
	// // #endif
	
	
	if(params.data){
		Object.assign(defaultData,params.data)
	}


    return new Promise((resolve, reject) => {
        uni.request({
            ...params,
            url: BASE_URL + params.url,
            method: params.method || 'POST',
            data: defaultData,
            // data: params.data || {},
            // dataType: "json",
            header: {
				'Content-Type':  'application/x-www-form-urlencoded',
                // 'Cookie': 'PHPSESSID='+uni.getStorageSync('token'),
                // 'token': uni.getStorageSync('token'),
                // 'Content-Type':  'application/json',
                // 'Accept': 'application/json',
            },
            success(res) {
				
                if (res.data.result == 400 && res.data.msg === '请登录') {
                    // getwxCode()
					console.log('需要静默授权');
					// vuex.state.showLoginTip = true
                } else if(res.data.code == -1 && res.data.message == '您尚未登录，请先进行登录' ){
					$Router.push({path: '/pages/login/login'})
				} else if(res.data.message == 'token已过期'){
					uni.showToast({
					    title: '登录失效',
					    duration: 3000,
					    icon: 'none'
					});
					$Router.push({path: '/pages/login/login'})	
				}else if(res.data.code != 0 ){
					uni.showToast({
					    title: res.data.message,
					    duration: 3000,
					    icon: 'none'
					});
				}else if(res.data.result ==0){
					uni.showToast({
					    title: res.data.msg,
					    duration: 3000,
					    icon: 'none'
					});
					
				}
				// else if (res.data.code != 1 && res.data.status != 200) {
                //     uni.showToast({
                //         title: '获取数据失败',
                //         duration: 3000,
                //         icon: 'error'
                //     });
                // }
                resolve(res.data)
            },
            fail(err) {
                uni.showToast({
                    title: '服务器错误',
                    duration: 3000,
                    icon: 'none'
                });
                reject(err)
            },
            complete() {
                // 隐藏加载中
                uni.hideLoading()
            }
        })
    })
}